﻿using System;
using System.Collections.Generic;

using NUnit.Framework;

using OpenQA.Selenium;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class D009_NewUserTrialQuarantine {
		private static IWebDriver driver;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public static void TestInitialize() {
			driver = TestHelper.GetDriver();

			// Log on as administrator!
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);

			// Disable multi-post anti-spam!
			driver.DisableMultiPost();

			// Enable new user trial, auto Quarantine, if below 2 approved posts
			driver.EnableNewUserTrial(2, new List<String>());

			driver.LogOff();

			driver.CreateUser(Constants.Users.Regular.Username, Constants.Users.Regular.Password, Constants.Users.Regular.Email);

			driver.LogOff();
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void NewTopicQuarantined() {
			String forumName = "Forum 2";
			driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password);

			String title = String.Format("My test topic {0} quarantine", DateTime.UtcNow);
			String content = "NEW USER TRIAL bla bla, shouldn't show up, but end up in Quarantine! The user is new, and hasn't posted before!";
			driver.CreateTopic(forumName, title, content);

			driver.GoToForum(forumName);
			Assert.AreEqual(false, driver.PageSource.Contains(title), "New user posted topic, should be quarantined, but is visible!");

			String title2 = String.Format("My test topic {0} quarantined", DateTime.UtcNow);
			content = "NEW USER TRIAL bla bla, shouldn't show up, but end up in Quarantine! The user is new, and hasn't posted before!";
			driver.CreateTopic(forumName, title2, content);

			driver.GoToForum(forumName);
			Assert.AreEqual(false, driver.PageSource.Contains(title), "New user posted topic, should be quarantined, but is visible!");

			driver.LogOff();

			driver.UnquarantineTopic(forumName, title);
			driver.UnquarantineTopic(forumName, title2);

			driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password);

			title = String.Format("My test topic {0} not quarantined", DateTime.UtcNow);
			content = "bla bla, should show up, The user is new, but has posted before!";
			driver.CreateTopic(forumName, title, content);

			driver.GoToForum(forumName);
			Assert.AreEqual(true, driver.PageSource.Contains(title), "New user posted topic, shouldn't be quarantined, but is was!");

			driver.LogOff();
		}
	}
}